#include "commerrno.h"
#include "CTreasureCfg.h"

void CTreasureCfg::InitConfig()
{
    _astTreasureCfg.m_iUsedCount = _astTreasureCfg.GetCount();

    for (int i = 0; i < _astTreasureCfg.Size(); ++i)
    {
        _astTreasureCfg[i].Clear();
    }

    _astTreasureUpCfg.m_iUsedCount = _astTreasureUpCfg.GetCount();

    for (int i = 0; i < _astTreasureUpCfg.Size(); ++i)
    {
        _astTreasureUpCfg[i].Clear();
    }
}

int CTreasureCfg::AddConfig(CTreasureCfgData& stCfgData)
{
    if (!stCfgData.IsValidID())
    {
        return ERROR_TREASURE_OVER_RESID;
    }

    if (_astTreasureCfg[stCfgData.GetResID() - 1].IsValidID()) 
    {
        return ERROR_TREASURE_REPEAT_RESID;
    }

    _astTreasureCfg[stCfgData.GetResID() - 1] = stCfgData;

    return 0;
}

int CTreasureCfg::FindConfig(int iResID)
{
    if (iResID > MAX_TREASURE_CFG_COUNT || iResID <= 0)
    {
        return -1;//因为返回下标 所以这里必须返回负数
    }

    if (!_astTreasureCfg[iResID - 1].IsValidID()) 
    {
        return -1;//因为返回下标 所以这里必须返回负数
    }

    if (iResID != _astTreasureCfg[iResID - 1].GetResID())
    {
        return -1;//因为返回下标 所以这里必须返回负数
    }

    return iResID - 1;
}

int CTreasureCfg::AddUpConfig(CTreasureUpCfgData& stCfgData)
{
    if (!stCfgData.IsValidLevel())
    {
        return ERROR_TREASURE_OVER_LEVEL;
    }

    if (_astTreasureUpCfg[stCfgData.GetLevel()].IsValidLevel()) 
    {
        return ERROR_TREASURE_REPEAT_LEVEL;
    }

    _astTreasureUpCfg[stCfgData.GetLevel()] = stCfgData;

    return 0;
}

int CTreasureCfg::FindUpConfig(short shLevel)
{
    if (shLevel >= MAX_TREASURE_UP_LEVEL || shLevel < 0)
    {
        return -1;//因为返回下标 所以这里必须返回负数
    }

    if (!_astTreasureUpCfg[shLevel].IsValidLevel()) 
    {
        return -1;//因为返回下标 所以这里必须返回负数
    }

    if (shLevel != _astTreasureUpCfg[shLevel].GetLevel())
    {
        return -1;//因为返回下标 所以这里必须返回负数
    }

    return shLevel;
}

